﻿using QuanLyThuVien.DTO;
using QuanLyThuVien.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;

namespace QuanLyThuVien.BLL
{
    //----------------------------------------------------------------------
    //Desc: class xử lý cho bảng PHIEUTRA
    //----------------------------------------------------------------------
    public class BLLPhieuTra
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public BLLPhieuTra()
        {
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách phiếu trả
        //----------------------------------------------------------------------
        public DataTable LayDanhSachPhieuTra()
        {
            DALPhieuTra _dalPhieuTra = new DALPhieuTra();
            return _dalPhieuTra.LayDanhSachPhieuTra();
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách phiếu trả của độc giả
        //----------------------------------------------------------------------
        public DataTable LayPhieuTraDocGia(string madocgia)
        {
            DALPhieuTra _dalPhieuTra = new DALPhieuTra();
            return _dalPhieuTra.LayPhieuTraDocGia(madocgia);
        }

        //----------------------------------------------------------------------
        //Desc: lấy phiếu trả
        //----------------------------------------------------------------------
        public DTOPhieuTra LayPhieuTra(string maphieutra)
        {
            DALPhieuTra _dalPhieuTra = new DALPhieuTra();
            DTOPhieuTra phieutra = _dalPhieuTra.LayPhieuTra(maphieutra);
            if (phieutra == null)
                return null;

            DALDocGia _dalDocGia = new DALDocGia();
            DTODocGia docgia = _dalDocGia.LayThongTinDG(phieutra.DocGia.MaDG);
            if (docgia == null)
                return null;

            DALCT_PhieuTra _dalCT_PhieuTra = new DALCT_PhieuTra();
            List<DTOCT_PhieuTra> list = _dalCT_PhieuTra.LayDanhSachCT_PhieuTra(phieutra.MaPhieuTra);
            if (list == null)
                return null;

            phieutra.DocGia = docgia;
            phieutra.DSCTPhieuTra = list;
            return phieutra;
        }

        //----------------------------------------------------------------------
        //Desc: phát sinh mã phiếu trả
        //----------------------------------------------------------------------
        public bool PhatSinhMaPhieuTra(ref DTOPhieuTra phieutra)
        {
            DALPhieuTra _dalPhieuTra = new DALPhieuTra();

            int stt = _dalPhieuTra.DemSoPhieuTra();
            if (stt < 0)
                return false;
            if (stt == 0)
                stt++;

            string maphieu = "PTR" + stt.ToString("D3");
            int dem = _dalPhieuTra.DemMaPhieuTra(maphieu);

            //có thể xảy ra vòng lặp vô hạn
            while (dem > 0)
            {
                stt++;
                maphieu = "PTR" + stt.ToString("D3");
                dem = _dalPhieuTra.DemMaPhieuTra(maphieu);
            }

            if (dem < 0)
                return false;

            phieutra.MaPhieuTra = maphieu;
            for (int i = 0; i < phieutra.DSCTPhieuTra.Count; i++ )
                phieutra.DSCTPhieuTra[i].MaPhieuTra = maphieu;

            return true;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật phiếu trả
        //----------------------------------------------------------------------
        public bool CapNhat(DTOPhieuTra phieutra)
        {
            DALPhieuTra _dalPhieuTra = new DALPhieuTra();
            return _dalPhieuTra.CapNhat(phieutra);
        }

    }
}
